// 登录服务（获取用户信息，登录，退出）
define('loginServers', ['angular','ngCookies', 'jquery'], function (angular,ngCookies, $) {
    var module = angular.module('servers',['ngCookies']);
    module.factory('loginServers', ['$http','$cookieStore', function ($http,$cookieStore) {
        var obj = {
            user: {},
            //登录
            login: function (account,password) {
                return $http({
                    method  : 'POST',
                    url: 'http://localhost:8888/api/users/login',
                    data:$.param({
                        "account":account,
                        "password":password
                    }),
                    cache:true,
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).then(function (response) {
                    if(response.data.length>0){
                        obj.user ={
                            user_id:response.data[0].id,
                            user_name:response.data[0].name
                        };
                        $cookieStore.put("user",obj.user);
                    }else{
                        obj.user={};
                    }
                    return obj.user;
                });
            },
            //退出
            logout: function () {
                obj.user={};
                $cookieStore.remove("user");
            },
            // 获取用户信息
            getUser:function(){
                var cookie_user=$cookieStore.get("user");
                if(angular.equals({},obj.user) && !angular.equals({},cookie_user)){
                    obj.user=cookie_user;
                }
                // }
                return obj.user;
            }
        };
        return obj;
    }]);
});